﻿
function extraInit()
{
    initTree(false, false, false);
}

function selectTreeNode(jobTitleId)
{
    viewWorktimeSheet(jobTitleId, 1);
}

function selectPage(index)
{
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, index);
}

function viewWorktimeSheet(jobTitleId, pageIndex)
{
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Salary/Sale/AjaxBasicSalary.aspx",
	    data : { 
		    "action" : "get_sale_basic_salary", 
		    "jobTitleId" : jobTitleId,
		    "pageIndex" : pageIndex
	    }, 
	    success : function (r) {
	        if (r.status)
			{
			    var table = $("#worktimeTable");
			    var rows = table.find(".div-row");
			    for (var i=rows.length-1; i>0; i--)
			    {
			        rows.eq(i).remove();
			    }
			    for (var i=0; i<r.list.length; i++)
			    {
			        var htmlRow = "<div class='div-row'>";
			        htmlRow += "<div class='div-cell c0'>"+r.list[i].order+"</div>";
			        htmlRow += "<div class='div-cell c4'>";
			        htmlRow += "<input type='hidden' name='hdfAgentId' value='"+r.list[i].agentId+"'/>";
			        htmlRow += "<span class='edit' onclick='editBasicSalary(this);'></span>";
			        htmlRow += r.list[i].fullName;
			        htmlRow += "</div>";
			        htmlRow += "<div class='div-cell c2'><span class='detail'>"+r.list[i].moneyLimit+"</span></div>";
			        htmlRow += "<div class='div-cell c2'><span class='detail'>"+r.list[i].baseSalary+"</span></div>";
			        htmlRow += "<div class='div-cell c2'><span class='detail2'>"+r.list[i].phone+"</span></div>";
			        htmlRow += "<div class='div-cell c2'><span class='detail2'>"+r.list[i].atmSerial+"</span></div>";
			        htmlRow += "<div class='div-cell c2'><span class='detail2'>"+r.list[i].bankName+"</span></div>";
			        htmlRow += "<div class='div-cell c9'><span class='detail3'>"+r.list[i].marketName+"</span></div>";
                    table.append(htmlRow);
			    }
			    
			    var divPaging = $("#divPaging");
			    var paging = "";
			    for (var i=1; i<=r.totalPage; i++)
			    {
			        if (r.pageIndex == i)
			        {
			            paging += "<span>"+i+"</span>";
			        }
			        else
			        {
			            paging += "<a href='javascript:void(0);' onclick='selectPage("+i+");'>"+i+"</a>";
			        }
			    }
			    divPaging.html(paging);
			}
	    }
    })
    .always(function() {
	    unmask(area);
	});
}

function editBasicSalary(button)
{
    var cells = $(button).closest(".div-row").find(".div-cell");
    var cellMoneyLimit = cells.eq(2);
    var cellBaseSalary = cells.eq(3);
    var cellPhone = cells.eq(4);
    var cellAtmSerial = cells.eq(5);
    var cellBank = cells.eq(6);
    var cellMarket = cells.eq(7);
    if (cellMoneyLimit.find("span").length > 0)
    {
        //change to edit mode
        var moneyLimit = unformat(cellMoneyLimit.find("span").text());
        var input = "<input type='text' name='moneyLimit' value='"+moneyLimit+"' onkeydown='enter(this, event);'/>";
        cellMoneyLimit.html(input);
        var baseSalary = unformat(cellBaseSalary.find("span").text());
        var input = "<input type='text' name='baseSalary' value='"+baseSalary+"' onkeydown='enter(this, event);'/>";
        cellBaseSalary.html(input);
        var phone = cellPhone.find("span").text();
        input = "<input type='text' name='phone' value='"+phone+"' onkeydown='enter(this, event);'/>";
        cellPhone.html(input);
        var atmSerial = cellAtmSerial.find("span").text();
        input = "<input type='text' name='atmSerial' value='"+atmSerial+"' onkeydown='enter(this, event);'/>";
        cellAtmSerial.html(input);
        var bank = cellBank.find("span").text();
        input = "<input type='text' name='bank' value='"+bank+"' onkeydown='enter(this, event);'/>";
        cellBank.html(input);
        var market = cellMarket.find("span").text();
        input = "<input type='text' name='market' value='"+market+"' onkeydown='enter(this, event);'/>";
        cellMarket.html(input);
        
        cellMoneyLimit.find("input").focus();
        
        var inputMarket = cellMarket.find("input[type=text]");
        var a = inputMarket.autocomplete({
            serviceUrl:'/Salary/Sale/AjaxBasicSalary.aspx',
            params:{action:'search_markets'},
            minChars:1,
            delimiter: /(,|;)\s*/, // regex or character
            dataType: 'json',
            maxHeight:400,
            width:800,
            zIndex: 9999,
            deferRequestBy: 1000, //miliseconds
            noCache: false, //default is false, set to true to disable caching
            // callback function:
            onSelect: function(suggestion){
                //alert('You selected: ' + suggestion.value + ', ' + suggestion.data.identifiedSerial);
                inputMarket.val(suggestion.data.fullName);
            }
            // local autosugest options:
            //lookup: [{value:'abc',data:'111'},{value:'list',data:'222'}],
            /*lookupFilter: function (suggestion, query, queryLowerCase)
            {
                return true;
            }*/
        });
    }
    else
    {
        //update data
        updateBasicSalary(cells);
    }
}

function enter(input, evt)
{
    if (evt.keyCode != 13)
    {
        return false;
    }
    var cells = $(input).closest(".div-row").find(".div-cell");
    updateBasicSalary(cells);
    
    return true;
}

function updateBasicSalary(cells)
{
    var cellMoneyLimit = cells.eq(2);
    var cellBaseSalary = cells.eq(3);
    var cellPhone = cells.eq(4);
    var cellAtmSerial = cells.eq(5);
    var cellBank = cells.eq(6);
    var cellMarket = cells.eq(7);
    var moneyLimit = cellMoneyLimit.find("input").val();
    var baseSalary = cellBaseSalary.find("input").val();
    var phone = cellPhone.find("input").val();
    var atmSerial = cellAtmSerial.find("input").val();
    var bank = cellBank.find("input").val();
    var marketName = cellMarket.find("input[name=market]").val();
    var agentId = cells.eq(1).find("input[name=hdfAgentId]").val();
   
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Salary/Sale/AjaxBasicSalary.aspx",
	    data : { 
		    "action" : "update_basic_salary", 
		    "agentId" : agentId,
		    "baseSalary" : baseSalary,
		    "moneyLimit" : moneyLimit,
		    "phone" : phone,
		    "atmSerial" : atmSerial,
		    "bank" : bank,
		    "marketName": marketName
	    }, 
	    success : function (r) {
	        if (r.status)
			{
			    var span = "<span class='detail'>"+r.moneyLimit+"</span>";
                cellMoneyLimit.html(span);
                span = "<span class='detail'>"+r.baseSalary+"</span>";
                cellBaseSalary.html(span);
                span = "<span class='detail2'>"+r.phone+"</span>";
                cellPhone.html(span);
                span = "<span class='detail2'>"+r.atmSerial+"</span>";
                cellAtmSerial.html(span);
                span = "<span class='detail2'>"+r.bankName+"</span>";
                cellBank.html(span);
                span = "<span class='detail3'>"+r.marketName+"</span>";
                cellMarket.html(span);
            }
            else
            {
                if (r.error != "")
                {
                    alert(r.error);
                }
            }
        }
    })
    .always(function() {
	    unmask(area);
	});
}

function exportExcel()
{
    var li = $("a.jstree-clicked").closest("li");
    if (li.length  == 0)
    {
        return;
    }
    $("#hdfJobTitleId").val(li.attr("id"));
}



